import os

from matplotlib import pyplot as plt

from utils import load_variable


def plot(_datas, _labels, _output_path):
    fig, ax = plt.subplots()
    # ax2 = plt.twinx(ax)

    for _data, _label in zip(_datas, _labels):
        x = _data['dataset_sizes']
        y = [each * 1000 for each in _data['avg_time_consumes']]
        # if _label == 'bruteforce':
        #     ax2.plot(x, y, marker='o', label=_label)
        #     continue
        ax.plot(x, y, marker='o', label=_label, c='#9933FA')
        # ax.plot(x, y, marker='o', label=_label)
    # ax.set_title('Time consumption of convex hull algorithms\non dataset of different sizes')
    ax.set_title('Time consumption of bruteforce convex hull algorithm\non dataset of different sizes')
    ax.set_xlabel('dataset size')
    ax.set_ylabel('time consume (ms)')
    plt.legend()

    # ### need to adjust ###
    # ax.set(xticks=_datas[0]['dataset_sizes'],
    #        ylim=(0, 400), yticks=range(0, 450, 50))
    ax.set(xticks=_datas[0]['dataset_sizes'],
           ylim=(0, 10000), yticks=range(0, 11000, 1000))
    plt.grid()


    plt.savefig(_output_path)
    if not _output_path.endswith('.svg'):
        if _output_path.endswith('.png'):
            _output_path = _output_path[:-4]
        plt.savefig(_output_path + '.svg')


if __name__ == '__main__':
    # names = ['graham_scan', 'graham_scan_self_quicksort', 'divide_and_conquer']
    # labels = ["graham scan using python's built-in sort",  'graham scan using handwritten quicksort',
    #           "divide and conquer"]
    names = ['bruteforce']
    labels = ['bruteforce']
    # output_path = 'results/result.png'
    output_path = 'results/result_bruteforce.png'
    datas = [load_variable(os.path.join('results', name, 'data.pkl')) for name in names]
    plot(datas, labels, output_path)
